package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.List;

import static java.lang.Thread.sleep;


public class Test {

    public static void main(String[] args) throws InterruptedException {
        //嵌套页面
//        innel();
        //点击
//        test01();
        //选择框
//        test02();
        //弹窗写字
//        test03();
        //选择文件
        test04();
    }

    private static void test04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("D:\\自己写的代码\\java3\\autotest2\\src\\main\\resources\\static\\test05.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("D:\\钉钉\\重复.jpg");
    }

    private static void test03() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("D:\\自己写的代码\\java3\\autotest2\\src\\main\\resources\\static\\test03.html");
        webDriver.findElement(By.cssSelector("body > button")).click();
        String name = "无敌暴龙战神";
        webDriver.switchTo().alert().sendKeys(name);
        sleep(3000);
        webDriver.switchTo().alert().accept();
        WebElement target = webDriver.findElement(By.cssSelector("body > div"));
        sleep(3000);
        if (target.getText().equals(name)){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }

    }

    private static void test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("D:\\自己写的代码\\java3\\autotest2\\src\\main\\resources\\static\\test02.html");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        sleep(3000);
        //通过下标来选择
//        select.selectByIndex(2);
        //通过value选择
        select.selectByValue("8.34");
        sleep(3000);
        webDriver.quit();
    }

    public static void test01() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("D:\\自己写的代码\\java3\\autotest2\\src\\main\\resources\\static\\test01.html");
        List<WebElement> webElementList = webDriver.findElements(By.cssSelector("input"));
        for (WebElement webElement : webElementList){
            if (webElement.getAttribute("type").equals("checkbox") || webElement.getAttribute("type").equals("radio")){
                webElement.click();
                sleep(3000);
            }
        }
        webDriver.quit();
    }

    private static void innel() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("D:\\自己写的代码\\java3\\autotest2\\src\\main\\resources\\static\\innel.html");
        sleep(3000);
        //通过frame进入嵌套页面，可通过id或者嵌套页面的层数
        webDriver.switchTo().frame("f2");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("端午节安康");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //webDriver.switchTo().frame("f1");
        //webDriver.findElement(By.cssSelector("body > div > div > a")).click();
//        sleep(3000);
//        webDriver.quit();
    }


}
